#############################################################################
#						Install Necessary Packages							#
#############################################################################
# source("http://gking.harvard.edu/zelig/install.R")
library(Zelig)
library(foreign)
library(xtable)
library(gee)
library(apsrtable)
library(Amelia)

#############################################################################
#						Set Mem and Load Raw Data							#
#############################################################################
memory.limit(size = 4000)
# Set the directory here
setwd("C:/Users/Gabe/Documents/My Dropbox/Gov 2001 Project")
rdata <- read.table("C:/Users/Gabe/Documents/My Dropbox/Gov 2001 Project/rdata.txt", sep="\t")

#############################################################################
#					Impute Missing Data using Amelia			  			#
#############################################################################	
num_imputs = 10				
idata <- amelia(	x = rdata, 
					cs = 		c(	"country2"), 
									
					idvars = 	c(	"hightech",
									"lowtech",
									"continent2", 
									"industryf",
									"tech_acquis_modef5"),
								
					ords = 		c(	"competition2",
									"advanced_technology",
									"govtservices"),
								
					logs = 		c(	"foreign_ownership", 
									"workforce_more12yrseduc", 
									"export_indreg", 
									"import_indreg", 
									"import1",
									"import2",
									"export1",
									"export2",
									"age"),
								
					m = num_imputs
				)			

#############################################################################
#				Insert Derivative Vars into Imputed Datasets			  	#
#############################################################################	
for (i in 1:num_imputs)	{

	import.contin = idata$imputations[[i]]$import1 + idata$imputations[[i]]$import2
	import.contin_sq = import.contin^2	
	export.contin = idata$imputations[[i]]$export1 + idata$imputations[[i]]$export2
	export.contin_sq = export.contin^2	
	agesq = idata$imputations[[i]]$age^2
	foreign_ownership_sq = idata$imputations[[i]]$foreign_ownership^2
	industrycountry <- as.factor( as.numeric(idata$imputations[[i]]$industryf) * 100 + as.numeric(idata$imputations[[i]]$country2))
	for_maj2 <- as.numeric((idata$imputations[[i]]$foreign_ownership >= .5))
	for_min <- as.numeric((idata$imputations[[i]]$foreign_ownership > 0) * (idata$imputations[[i]]$foreign_ownership < .5))
	exporters <- as.numeric(export.contin > 0)
	importers <- as.numeric(import.contin > 0)
	domestic <- idata$imputations[[i]]$foreign_ownership == 0
	
	idata$imputations[[i]] <- cbind(	idata$imputations[[i]],
										import.contin,
										import.contin_sq,
										export.contin,
										export.contin_sq,
										agesq,
										foreign_ownership_sq,
										industrycountry,
										for_maj2,
										for_min,
										exporters, 
										importers,
										domestic
									)
}

#############################################################################
#			Insert Derivative Vars into Pairwise Deleted Dataset			#
#############################################################################	
pwddata <- rdata

for(i in 1:length(pwddata$process_innov)) {
	if(is.na(pwddata$import1[i]))
		pwddata$import1[i] <- 0
	if(is.na(pwddata$import2[i]))
		pwddata$import2[i] <- 0
}

for(i in 1:length(pwddata$process_innov)) {
	if(is.na(pwddata$export1[i]))
		pwddata$export1[i] <- 0
	if(is.na(pwddata$export2[i]))
		pwddata$export2[i] <- 0
}

	pwddata$import.contin = (pwddata$import1) + (pwddata$import2)
	pwddata$import.contin_sq = import.contin^2	
	pwddata$export.contin = pwddata$export1 + pwddata$export2
	pwddata$export.contin_sq = export.contin^2	 
	pwddata$agesq = pwddata$age^2
	
	pwddata$foreign_ownership_sq = pwddata$foreign_ownership^2
	pwddata$industrycountry <- as.factor( as.numeric(pwddata$industryf) * 100 + as.numeric(pwddata$country2))
	pwddata$for_maj2 <- as.numeric((pwddata$foreign_ownership >= .5))
	pwddata$for_min <- as.numeric((pwddata$foreign_ownership > 0) * (pwddata$foreign_ownership < .5))
	
	pwddata$exporters <- as.numeric(export.contin > 0)
	pwddata$importers <- as.numeric(import.contin > 0)
	pwddata$domestic <- as.numeric(pwddata$foreign_ownership == 0)


# idata: imputed datasets
# pwddata: pairwise-deleted data	
					
